package com.ruoyi.finance.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.finance.domain.VoucherTemplate;
import com.ruoyi.finance.domain.VoucherTemplateType;
import com.ruoyi.finance.domain.InvoiceDetailTable;
import com.ruoyi.finance.mapper.InvoiceDetailTableMapper;
import com.ruoyi.finance.mapper.InvoiceGoodsMapper;
import com.ruoyi.finance.mapper.VoucherTemplateMapper;
import com.ruoyi.finance.vo.IncomingDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.IncomingInvoiceMapper;
import com.ruoyi.finance.domain.IncomingInvoice;
import com.ruoyi.finance.service.IIncomingInvoiceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销项发票Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@Service
public class IncomingInvoiceServiceImpl implements IIncomingInvoiceService
{
    @Autowired
    private IncomingInvoiceMapper incomingInvoiceMapper;

    @Autowired
    private VoucherTemplateMapper voucherTemplateMapper;

    @Autowired
    private InvoiceGoodsMapper invoiceGoodsMapper;
    @Autowired
    private InvoiceDetailTableMapper invoiceDetailTableMapper;

    /**
     * 查询销项发票
     *
     * @param id 销项发票主键
     * @return 销项发票
     */
    @Override
    public IncomingInvoice selectIncomingInvoiceById(Long id)
    {
        return incomingInvoiceMapper.selectIncomingInvoiceById(id);
    }

    /**
     * 查询销项发票列表
     *
     * @param incomingInvoice 销项发票
     * @return 销项发票
     */
    @Override
    public List<IncomingInvoice> selectIncomingInvoiceList(IncomingInvoice incomingInvoice)
    {
        return incomingInvoiceMapper.selectIncomingInvoiceList(incomingInvoice);
    }

    /**
     * 新增销项发票
     *
     * @param incomingInvoice 销项发票
     * @return 结果
     */
    @Override
    public int insertIncomingInvoice(IncomingInvoice incomingInvoice)
    {
        incomingInvoice.setCreateTime(DateUtils.getNowDate());
        return incomingInvoiceMapper.insertIncomingInvoice(incomingInvoice);
    }

    /**
     * 修改销项发票
     *
     * @param incomingInvoice 销项发票
     * @return 结果
     */
    @Override
    public int updateIncomingInvoice(IncomingInvoice incomingInvoice)
    {
        incomingInvoice.setUpdateTime(DateUtils.getNowDate());
        return incomingInvoiceMapper.updateIncomingInvoice(incomingInvoice);
    }

    /**
     * 批量删除销项发票
     *
     * @param ids 需要删除的销项发票主键
     * @return 结果
     */
    @Override
    public int deleteIncomingInvoiceByIds(Long[] ids)
    {
        return incomingInvoiceMapper.deleteIncomingInvoiceByIds(ids);
    }

    /**
     * 删除销项发票信息
     *
     * @param id 销项发票主键
     * @return 结果
     */
    @Override
    public int deleteIncomingInvoiceById(Long id)
    {
        return incomingInvoiceMapper.deleteIncomingInvoiceById(id);
    }

    @Override
    public List<VoucherTemplateType> VoucherTemplateTypelist() {
        return incomingInvoiceMapper.VoucherTemplateTypelist();
    }

    @Override
    public List<VoucherTemplate> VoucherTemplatelist() {
        return incomingInvoiceMapper.VoucherTemplatelist();
    }

    @Override
    public IncomingInvoice selectvoucherTemplate(Integer templateId) {
        return incomingInvoiceMapper.selectvoucherTemplate(templateId);
    }

    @Override
    @Transactional
    public String incomingInvoice(IncomingDTO incomingDTO) {

        if (incomingDTO == null) {
            throw new IllegalArgumentException("传入的IncomingDTO对象不能为空");
        }
        if (incomingDTO.getInvoiceDetailTableList() == null || incomingDTO.getInvoiceDetailTableList().isEmpty()) {
            throw new IllegalArgumentException("传入的IncomingDTO对象中的invoiceDetailTableList属性不能为空或空列表");
        }

        // 创建并填充IncomingInvoice对象
        IncomingInvoice incomingInvoice = new IncomingInvoice();
        BeanUtils.copyProperties(incomingDTO, incomingInvoice);
        incomingInvoice.setInvoiceType(incomingDTO.getId());
        incomingInvoice.setInvoiceDate(incomingDTO.getInvoiceDate());
        incomingInvoice.setInvoiceDaima(incomingDTO.getInvoiceDaima());
        incomingInvoice.setSupplierName(incomingDTO.getSupplierName());
        incomingInvoice.setInvoiceNamed(incomingDTO.getInvoiceNamed());
        incomingInvoice.setInvoiceAddress(incomingDTO.getInvoiceAddress());
        incomingInvoice.setInvoicePhone(incomingDTO.getInvoicePhone());
        incomingInvoice.setBankOfDeposit(incomingDTO.getBankOfDeposit());
        incomingInvoice.setAccountNumber(incomingDTO.getAccountNumber());
        incomingInvoice.setVerificationStatus(incomingDTO.getVerificationStatus());
        incomingInvoice.setVerificationDate(incomingDTO.getVerificationDate());
        incomingInvoice.setInvoiceRemark(incomingDTO.getInvoiceRemark());

        // 插入IncomingInvoice记录
        int incomingInvoiceInsertResult = incomingInvoiceMapper.insertIncomingInvoice(incomingInvoice);
        if (incomingInvoiceInsertResult <= 0) {
            throw new RuntimeException("插入IncomingInvoice记录失败");
        }

        // 创建并填充VoucherTemplate对象
        VoucherTemplate voucherTemplate = new VoucherTemplate();
        BeanUtils.copyProperties(incomingDTO, voucherTemplate);
        voucherTemplate.setTypeId(incomingDTO.getTypeId());

        // 查询并设置VoucherTemplate的模板名称
        VoucherTemplate voucherTemplate1 = voucherTemplateMapper.selectVoucherTemplateByTemplateId(incomingDTO.getTemplateId());
        if (voucherTemplate1 == null) {
            throw new RuntimeException("未找到对应的VoucherTemplate记录");
        }
        String templateName = voucherTemplate1.getTemplateName();
        voucherTemplate.setTemplateName(templateName);

        // 插入VoucherTemplate记录
        int voucherTemplateInsertResult = voucherTemplateMapper.insertVoucherTemplate(voucherTemplate);
        if (voucherTemplateInsertResult <= 0) {
            throw new RuntimeException("插入VoucherTemplate记录失败");
        }

        // 处理InvoiceDetailTable列表
        List<InvoiceDetailTable> invoiceDetailTableList = incomingDTO.getInvoiceDetailTableList();
        for (InvoiceDetailTable invoiceDetailTable : invoiceDetailTableList) {
            InvoiceDetailTable invoiceDetailTable1 = new InvoiceDetailTable();
            invoiceDetailTable1.setInvoiceTypeId(incomingDTO.getId());
            invoiceDetailTable1.setGoodsName(invoiceDetailTable.getGoodsName());
            invoiceDetailTable1.setSpecificationModel(invoiceDetailTable.getSpecificationModel());
            invoiceDetailTable1.setUnit(invoiceDetailTable.getUnit());
            invoiceDetailTable1.setQuantity(invoiceDetailTable.getQuantity());
            invoiceDetailTable1.setUnitPrice(invoiceDetailTable.getUnitPrice());
            invoiceDetailTable1.setAmount(invoiceDetailTable.getAmount());
            invoiceDetailTable1.setTaxRate(invoiceDetailTable.getTaxRate());
            invoiceDetailTable1.setTaxAmount(invoiceDetailTable.getTaxAmount());
            invoiceDetailTable1.setTaxationItem(invoiceDetailTable.getTaxationItem());
            invoiceDetailTable1.setTaxCalculationMethod(invoiceDetailTable.getTaxCalculationMethod());
            BeanUtils.copyProperties(invoiceDetailTable, invoiceDetailTable1);

            // 插入InvoiceDetailTable记录
            int invoiceDetailTableInsertResult = invoiceDetailTableMapper.insertInvoiceDetailTable(invoiceDetailTable1);
            if (invoiceDetailTableInsertResult <= 0) {
                throw new RuntimeException("插入InvoiceDetailTable记录失败");
            }
        }

        return "添加成功";
    }
}
